package com.lin.consume;

import com.corundumstudio.socketio.BroadcastOperations;
import com.corundumstudio.socketio.SocketIONamespace;
import com.lin.common.constant.CustomerServiceConstant;
import com.lin.common.constant.SocketIoConstant;
import com.lin.common.error.CustomRuntimeException;
import com.lin.po.Chats;
import com.lin.socketio.dto.ChatObject;
import com.lin.socketio.dto.MessageBase;
import com.lin.service.chats.ChatsService;
import com.lin.service.translate.TranslateService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Component(RocketMqConstant.CHATS_TOPIC + CustomerServiceConstant.CONSUMER_HANDLER)
public class ChatsConsumer implements ConsumerHandler {
    private static Logger log = LoggerFactory.getLogger(ChatsConsumer.class);
    @Autowired
    private SocketIONamespace socketIOChats;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ChatsService chatsService;

    @Autowired
    private TranslateService translateService;

    @Value("${" + CustomerServiceConstant.content_topic + "}")
    private String content;

    public void onMessage(MessageBase msgExt) throws CustomRuntimeException, UnsupportedEncodingException {
        ChatObject co = (ChatObject) msgExt;
        log.info("ChatsConsumer on message group:{} body :{}", co.getCustomerGroupId(), co.toString());
        socketIOChats.getRoomOperations(co.getCustomerGroupId() + "").sendEvent(SocketIoConstant.CHAT_EVENT_NAME, co);
        //TODO 删除下面的打印内容
//        BroadcastOperations bo = socketIOChats.getRoomOperations(co.getCustomerGroupId() + "");
//        bo.getClients().forEach(i -> {
//            log.info("rabbitmq receive Session ID  :{}，AllRooms：{}", i.getSessionId(), i.getAllRooms());
//        });
        //==============================
        //非撤销
//        if(!StringUtils.equals(co.getReadFlag(), Chats.READ_FLAG_CANCEL)){
//            //TODO 如果设置翻译，则需要把发出的内容进行翻译，
//            translateService.translate(co);
//
//            socketIOChats.getRoomOperations(co.getCustomerGroupId() + "").sendEvent(SocketIoConstant.CHAT_EVENT_NAME, co);
//
//            //保存出现错误也能继续发送
//            try {
//                chatsService.save(co);
//            } catch (IOException e) {
//                log.error("save ChatObject error", e);
//            }
//        }else{//撤销场景
//            co.setContent(co.getSender()+"撤回了一条信息。");
//            socketIOChats.getRoomOperations(co.getCustomerGroupId() + "").sendEvent(SocketIoConstant.CHAT_EVENT_NAME, co);
//            //保存出现错误也能继续发送
//            try {
//                chatsService.save(co);
//            } catch (IOException e) {
//                log.error("save ChatObject error", e);
//            }
//        }

        log.info("send client success");
    }


}
